package org.zs.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.zs.tool.ConfigTool;


public abstract class BaseDAO<T> {
	
	
	protected ConfigTool config = ConfigTool.getTool();
	public BaseDAO(){
		init();
	}
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("加载数据库驱动失败");
		}
		
	}
	private void init()
	{
		try {
			conn = DriverManager.getConnection(config.getURL(), config.getUser(), config.getPassword());
		} catch (SQLException e) {
			System.out.println("error:=====数据库连接失败");
		}
	}
	protected Connection conn;
	
	public abstract T find(Serializable id);
	
	public abstract boolean remove(Serializable id);
	
	public abstract T add(T entity);
	
	public abstract T update(T entity);
	
	public abstract T getEntityByRS(ResultSet rs);
	
	protected PreparedStatement getSqlPt(String sql)
	{
		if(conn==null)
		{
			if(config.debug())
			{
				System.out.println("debug:==========试图用空连接来进行查询,sql:"+sql);
			}
			return null;
		}
			
		PreparedStatement pt = null;
		try {
			pt = conn.prepareStatement(sql);
		} catch (SQLException e) {
			System.out.println("error:=====创建PreparedStatement失败");
		}
		return pt;
	}
	
	protected ResultSet result(PreparedStatement pt)
	{
		ResultSet rs = null;
		try {
			 rs = pt.executeQuery();
		} catch (Exception e) {
			System.out.println("error:=====获取resultSet时错误");
		}
		return rs;
	}
	/**
	 * 获取刚插入的主键
	 * @param pt
	 * @return
	 */
	protected int resultForId(PreparedStatement pt)
	{
		int id = 0;
		ResultSet rs = null;
		try {
			pt.executeUpdate();
			rs = pt.getGeneratedKeys();
			if(rs.next())
			{
				id = rs.getInt(1);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
	
	
	protected boolean rsNext(ResultSet rs)
	{
		boolean tmp = false;
		try {
			 tmp = rs.next();
		} catch (SQLException e) {
			System.out.println("error:=====resultSet next时出错");
		}
		return tmp;
	}
	
	protected void setInt(Serializable parm,PreparedStatement pt,int i)
	{
		try {
			int p = Integer.parseInt(parm.toString());
			pt.setInt(i,p);
		} catch (Exception e) {
			if(config.debug())
			{
				System.out.println("debug:==========设置Statement参数失败.参数:"+parm+",类型int,索引:"+i);
			}
		}
	}
	
	protected void setString(Serializable parm,PreparedStatement pt,int i)
	{
		try {
			pt.setString(i, parm.toString());
		} catch (Exception e) {
			if(config.debug())
			{
				System.out.println("debug:==========设置Statement参数失败.参数:"+parm+",类型string,索引:"+i);
			}
		}
	}
	
	
	protected void close()
	{
		try {
			conn.close();
			conn = null;
		} catch (SQLException e) {
			System.out.println("error:=====数据库连接释放失败");
		}
	}
	
	protected void close(ResultSet rs)
	{
		try {
			rs.close();
		} catch (SQLException e) {
			if(config.debug())
			{
				System.out.println("debug:==========Resultset关闭时出错");
			}
		}
	}
	protected void close(PreparedStatement pt)
	{
		try {
			pt.close();
		} catch (SQLException e) {
			if(config.debug())
			{
				System.out.println("debug:==========PreparedStatement关闭时出错");
			}
		}
	}
	

}
